package com.ihighest.app.infox.activity;

import org.json.JSONException;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.ihighest.app.common.api.Api;
import com.ihighest.app.common.api.ApiCallback;
import com.ihighest.app.common.api.ApiParam;
import com.ihighest.app.common.api.ApiResult;
import com.ihighest.app.common.kit.AppKit;
import com.ihighest.app.infox.R;
import com.ihighest.app.infox.myview.ActivityTitle;

/**
 * 会员详情界面
 * 
 * @author lkx Created at 2015-11-20 上午9:37:45
 * @version 1.0
 */
public class UserDetailActivity extends BaseActivity {
	private ActivityTitle title;
	private ImageView userDetailImg;
	private TextView userDetailName, UserDetailType, answerNumber, acceptNumber, agreeNumber;
	private WebView detailWeb;
	private String userId;
	private ImageView userDetailRankImg;
	private TextView askTa;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.infox_activity_userdetail);
		userId = getIntent().getStringExtra("userId");
		initView();
		getData(userId);
	}

	/**
	 * 获取用户数据
	 * 
	 * @param userId2
	 * 
	 * @author lkx Created at 2015-11-19 下午4:22:57
	 */
	private void getData(String userId2) {
		Api.get("me/searchDetails", ApiParam.create().addParam("id", userId2).addParam("type", "1"), new ApiCallback(
				UserDetailActivity.this) {

			private String imgurl;

			@Override
			public void handleSuccess(ApiResult result) {
				try {
					String name = result.getData().getString("name");
					userDetailName.setText(name);
					if (name.length() >= 5) {
						title.setTitle(name.substring(0, 5) + "... 的名片");
					} else {
						title.setTitle(name + " 的名片");
					}
					acceptNumber.setText(result.getData().getInt("accept") + "");
					agreeNumber.setText(result.getData().getInt("agree") + "");
					answerNumber.setText(result.getData().getInt("answer") + "");
					imgurl = result.getData().getString("header");
					// imgurl = result.getData().getString("header") + "?" +
					// System.currentTimeMillis();

					// DisplayImageOptions defaultOptions = new
					// DisplayImageOptions.Builder()
					// .showImageForEmptyUri(R.drawable.infox_person_no_icon).showImageOnFail(R.drawable.infox_person_no_icon)
					// .showImageOnLoading(R.drawable.infox_person_no_icon).cacheInMemory(true).cacheOnDisc(true).build();
					// ImageLoader.getInstance().displayImage(imgurl,
					// userDetailImg, defaultOptions);
					AppKit.SetUserImg(imgurl, userDetailImg);
					if ("Specialist".equalsIgnoreCase(result.getData().getString("type"))) {
						userDetailRankImg.setVisibility(View.VISIBLE);
						UserDetailType.setText(result.getData().getString("rank"));
						AppKit.SetUserImg(result.getData().getString("rankImage"), userDetailRankImg);
						// ImageLoader.getInstance().displayImage(result.getData().getString("rankImage"),
						// userDetailRankImg, defaultOptions);
					} else {
						UserDetailType.setText("");
						userDetailRankImg.setVisibility(View.INVISIBLE);
					}
					String content = result.getData().getString("myInfo");
					String mContnet = "<font color='#6A6A6A'>" + content + "</font>";
					detailWeb.loadDataWithBaseURL(null, mContnet, "text/html", "utf-8", null);
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}

			@Override
			public void handleFailure(Exception e, String msg) {
				detailWeb.loadDataWithBaseURL(null, "数据加载失败", "text/html", "utf-8", null);

			}
		}, UserDetailActivity.this);
	}

	/**
	 * 绑定控件
	 * 
	 * 
	 * @author lkx Created at 2015-11-19 下午3:46:48
	 */
	@SuppressLint("SetJavaScriptEnabled")
	private void initView() {
		title = (ActivityTitle) findViewById(R.id.userDetailtitle);
		title.setTitle("");
		title.setViewGone(title.titleSetting, title.titleRefresh, title.titleMsg, title.rightLayout);
		title.setViewVersible(title.titleBack);
		userDetailName = (TextView) findViewById(R.id.userDetailName);
		UserDetailType = (TextView) findViewById(R.id.userDetailType);
		answerNumber = (TextView) findViewById(R.id.userDetailAnswerCount);
		acceptNumber = (TextView) findViewById(R.id.userDetailAcceptCount);
		agreeNumber = (TextView) findViewById(R.id.userDetailAgreeCount);
		userDetailImg = (ImageView) findViewById(R.id.userImg);
		detailWeb = (WebView) findViewById(R.id.detailWebview);
		userDetailRankImg = (ImageView) findViewById(R.id.userDetailRankImg);
		askTa = (TextView) findViewById(R.id.askTA);
		detailWeb.getSettings().setJavaScriptEnabled(true);
		detailWeb.getSettings().setBuiltInZoomControls(false);
		// 设置可以支持缩放
		detailWeb.getSettings().setSupportZoom(false);
		// webView.getSettings().setBlockNetworkImage(true);
		// webView.getSettings().setLoadWithOverviewMode(true);
		// webView.getSettings().setSupportMultipleWindows(true);
		// 自适应屏幕
		detailWeb.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
		askTa.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(UserDetailActivity.this, AskQuestionActivity.class);
				intent.putExtra("userId", userId);
				startActivity(intent);

			}
		});

	}
}
